package models

import (
	"github.com/olivere/elastic/v7"
	"github.com/redis/go-redis/v9"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"log"
)

var db *gorm.DB
var EsClient *elastic.Client
var Redis *redis.Client

func init() {
	var err error
	dsn := "root:root@tcp(127.0.0.1:3306)/ping?charset=utf8mb4&parseTime=True&loc=Local"
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		log.Println("数据库连接失败", err)
		return
	}
	err = db.AutoMigrate()
	if err != nil {
		log.Println("数据表连接失败", err)
		return
	}

	EsClient, err = elastic.NewClient(elastic.SetURL("127.0.0.1:9277"), elastic.SetSniff(false))
	if err != nil {
		log.Println("es连接失败", err)
		return
	}

	Redis = redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
		DB:   0,
	})

}
